﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using H2.NET.Web.Workflow.Services;
using AutoMapper;
using H2.NET.Business.Core;

namespace H2.NET.Web.Management.Permission
{
    /// <summary>
    /// Handler 的摘要说明
    /// </summary>
    public class Handler : IHttpHandler
    {

        private readonly PermissionService _permissionService = new PermissionService();

        public void ProcessRequest(HttpContext context)
        {
            var action = context.Request.QueryString["action"];
            switch (action)
            {
                case "GetData":
                    GetData(context);
                    break;
                default:
                    break;

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private void GetData(HttpContext context)
        {
            Mapper.CreateMap<t_mdc_Permission, H2.NET.Web.Workflow.Models.Permission>();

            var permissions =
                H2.NET.Web.Workflow.Helpers.ConvertHelper<t_mdc_Permission>.ConvertToList(
                    _permissionService.GetList("").Tables[0]);

            var list = new List<H2.NET.Web.Workflow.Models.Permission>();


            var parents = permissions.Where(t => t.ParentID == null).ToList();
            foreach (var permission in parents)
            {
                var model = Mapper.Map<t_mdc_Permission, H2.NET.Web.Workflow.Models.Permission>(permission);

                var children = permissions.Where(t => t.ParentID == model.ID).ToList();

                if (children.Any())
                {
                    if (model.Children == null)
                        model.Children = new List<H2.NET.Web.Workflow.Models.Permission>();
                    model.Children.AddRange(CovertPermissions(permissions, children));
                }

                list.Add(model);
            }

            var setting = new Newtonsoft.Json.JsonSerializerSettings
            {
                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
            };

            list = list.OrderBy(t => t.Order).ToList();

            var perms = new H2.NET.Web.Workflow.Models.GridData<H2.NET.Web.Workflow.Models.Permission>(list);

            var content = Newtonsoft.Json.JsonConvert.SerializeObject(perms, setting);


            context.Response.Clear();
            context.Response.ContentType = "text/plain";
            context.Response.Write(content.Replace("Children", "children"));
            context.Response.End();
        }

        private List<H2.NET.Web.Workflow.Models.Permission> CovertPermissions(List<t_mdc_Permission> allpermissions, List<t_mdc_Permission> permissions)
        {
            var list = new List<H2.NET.Web.Workflow.Models.Permission>();
            foreach (var permission in permissions)
            {
                var model = Mapper.Map<t_mdc_Permission, H2.NET.Web.Workflow.Models.Permission>(permission);
                var children = allpermissions.Where(t => t.ParentID == model.ID).ToList();
                if (children.Any())
                {
                    if (model.Children == null)
                        model.Children = new List<H2.NET.Web.Workflow.Models.Permission>();
                    model.Children.AddRange(CovertPermissions(allpermissions, children));
                }

                list.Add(model);
            }

            return list;
        }
    }
}